﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using VINETLIFE.DO;
using VINETLIFE.BL;
using System.Web.Script.Services;
using System.Data;

namespace VINETLIFE.Module
{
    /// <summary>
    /// Summary description for AddCartSV
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]

    public class AddCartSV : System.Web.Services.WebService
    {

        public AddCartSV()
        {

        }

        [WebMethod(EnableSession = true)]
        public bool AddSesson(int prdID, int qty)
        {
            if (qty == 0) qty = 1;
            cmsProductDO itemDO = new cmsProductDO();
            itemDO.ProductID = prdID;
            //itemDO.ProductID = idProduct;
            cmsProductDO product = new cmsProductBL().Select(itemDO);
            if (product != null)
            {
                ProductShop item = new ProductShop();
                item.Id = product.ProductID;
                item.Image = product.ProductImage;
                item.Name = product.ProductTitle;
                item.ProductCode = product.ProductCode;
                if (product.IsPromotion) //  Kiểm tra sản phẩm có khuyến mãi thì tính giá khuyến mãi(saleprice). ko thì tính giá bình thường(price)
                {
                    if (product.SalePrice == 0 || product.SalePrice.ToString().Equals(""))
                    {
                        item.Cost = 0;
                    }
                    else
                    {
                        item.Cost = (decimal)product.SalePrice;
                    }
                }
                // Sản phẩm không có giá khuyến mãi
                else
                {
                    if (product.Price == 0 || product.Price.ToString().Equals(""))
                        item.Cost = 0;
                    else
                    {

                        item.Cost = (decimal)product.Price;
                    }
                }
                if (product.MemberPrice == 0)
                {
                    item.MemberCost = item.Cost;
                }
                else
                {
                    item.MemberCost = (decimal)product.MemberPrice;
                }
                item.Quantity = qty;
                ((MyShopCart)Session["shop"]).Add(item);
            }
            return true;
        }
        //      MyShopCart myShopCartHotdeal = new MyShopCart();
        [WebMethod(EnableSession = true)]
        public bool AddSessonHotdeal(int prdID, int qty, string name, string phone, string email, string dateOf, string address)
        {
            if (qty == 0) qty = 1;
            cmsProductDO itemDO = new cmsProductDO();
            itemDO.ProductID = prdID;
            //itemDO.ProductID = idProduct;
            MyShopCart myShop = new MyShopCart();
            cmsProductDO product = new cmsProductBL().Select(itemDO);
            if (product != null)
            {
                ProductShop item = new ProductShop();
                item.Id = product.ProductID;
                item.Image = product.ProductImage;
                item.Name = product.ProductTitle;
                if (product.IsPromotion) //  Kiểm tra sản phẩm có khuyến mãi thì tính giá khuyến mãi(saleprice). ko thì tính giá bình thường(price)
                {
                    if (product.SalePrice == 0 || product.SalePrice.ToString().Equals(""))
                    {
                        item.Cost = 0;
                    }
                    else
                    {
                        item.Cost = (decimal)product.SalePrice;
                    }
                }
                // Sản phẩm không có giá khuyến mãi
                else
                {
                    if (product.Price == 0 || product.Price.ToString().Equals(""))
                        item.Cost = 0;
                    else
                    {

                        item.Cost = (decimal)product.Price;
                    }
                }
                item.Quantity = qty;
                //   myShopCartHotdeal.Add(item);
                ((MyShopCart)Session["shopx"]).Add(item);
                //    SendMail(myShop, name, address, phone, email,dateOf);
            }
            return true;
        }
        //void SendMail(MyShopCart myShop, string name, string address, string phone, string email, string dateOf)
        //{
        //    DateTime now = DateTime.Now;
        //    string date = now.Day + " - " + now.Month + " - " + now.Year;

        //    //MyShopCart myShop = (MyShopCart)Session["shop"];
        //    string totalCost = String.Format("{0:0,0}", myShop.GetTotalCost());

        //    string content = "<div style='margin-top:10px'>";
        //    content += "<fieldset>";
        //    content += "<legend>Thông tin đặt hàng</legend>";
        //    content += " <table width='100%' border='1' cellpadding='3' cellspacing='0' style='border-collapse:collapse'>";
        //    content += "<tbody><tr bgcolor='#999999' style='color:#FFF'><td colspan='2'>Thông tin khách hàng </td></tr>";
        //    content += "<tr><td style='width:100px'>Họ tên : </td><td>" + name + "</td></tr>";
        //    content += "<tr><td>Địa chỉ : </td><td>" + address + "</td></tr>";
        //    content += "<tr><td>Điện thoại : </td><td>" + phone + "</td></tr> ";
        //    content += "<tr><td>Email : </td><td>" + email + "</td></tr>";
        //    content += "<tr><td>Ngày nhận hàng : </td><td>" + dateOf + "</td></tr>";
        //    //content += "<tr><td>Ghi chú : </td><td>" + txtNotes.Text + "</td></tr>";
        //    content += "<tr bgcolor='#999999' style='color:#FFF'><td colspan='2'>Thông tin sản phẩm đặt hàng </td></tr>";
        //    int count_item = 1;
        //    foreach (var item in myShop.ListProduct)
        //    {
        //        content += "<tr><td style='width:100px'>Sản phẩm " + count_item + "</td><td>" + item.Name + " - Mã : " + item.Id + " - Số lượng : " + item.Quantity + " - Giá : " + String.Format("{0:0,0}", item.Cost) + "</td></tr>";
        //        count_item++;
        //    }
        //    content += "<tr bgcolor='#999999' style='color:#FFF'><td colspan='2'>Tổng giá trị : " + totalCost + "</td></tr>";
        //    content += "</table>";
        //    content += "<p>Ngày gửi : " + date + "</p></fieldset></div>";
        //    string AdminEmail = new sysConfigBL().Select(new sysConfigDO() { ConfigID = 7 }).ConfigValue;
        //    Myfunction.SendMail(strFromMail, strFromPass, "Đơn đặt hàng của khách hàng: " + name, AdminEmail, "Đơn đặt hàng ngày " + now.ToString("dd/MM/yyyy HH:mm t"), content);
        //}
        [WebMethod(EnableSession = true)]
        public string rtTotalHotdeal()
        {
            MyShopCart cart = new MyShopCart();
            cart = (MyShopCart)Session["shopx"];
            return string.Format("{0:0,0}", cart.GetTotalCost());
        }
        [WebMethod(EnableSession = true)]
        public string rtNumberHotdeal()
        {
            MyShopCart cart = new MyShopCart();
            cart = (MyShopCart)Session["shopx"];
            return cart.GetTotalProduct().ToString();

        }
        [WebMethod(EnableSession = true)]
        public string rtTotal()
        {
            MyShopCart cart = new MyShopCart();
            cart = (MyShopCart)Session["shop"];
            return string.Format("{0:0,0}", cart.GetTotalCost());
        }
        [WebMethod(EnableSession = true)]
        public string rtNumber()
        {
            MyShopCart cart = new MyShopCart();
            cart = (MyShopCart)Session["shop"];
            return cart.GetTotalProduct().ToString();

        }
        [WebMethod(EnableSession = true)]
        public string loginX(string userName, string passWord, string saveData)
        {

            string sPass = VINETLIFE.AdminCP.Functions.EncryptMd5(passWord);
            DataTable dtUser = new cmsCustomerBL().SelectLogin(userName, sPass);
            if (dtUser.Rows.Count > 0)
            {
                Session["FullName"] = dtUser.Rows[0]["FullName"].ToString();
                Session["UserName"] = dtUser.Rows[0]["UserName"].ToString();
                Session["CustomerId"] = dtUser.Rows[0]["ID"].ToString();
                MailFixedGearStyleX(int.Parse(Session["CustomerId"].ToString()), saveData);
                return "1";
            }
            else
            {
                return "2";
            }
        }
        [WebMethod(EnableSession = true)]
        public int Login(string userName, string password)
        {
            string sPass = VINETLIFE.AdminCP.Functions.EncryptMd5(password);

            DataTable dtUser = new cmsCustomerBL().SelectLogin(userName, sPass);
            if (dtUser.Rows.Count > 0)
            {
                int customerId = int.Parse(dtUser.Rows[0]["ID"].ToString());
                Session["FullName"] = dtUser.Rows[0]["FullName"].ToString();
                Session["UserName"] = dtUser.Rows[0]["UserName"].ToString();
                Session["CustomerId"] = dtUser.Rows[0]["ID"].ToString();
                return 1;
            }
            else
            {
                return 0;
            }
        }
        [WebMethod(EnableSession = true)]
        public int Logout()
        {
            Session["FullName"] = null;
            Session["UserName"] = null;
            Session["CustomerId"] = null;
            Session.Abandon();
            return 1;
        }
        [WebMethod(EnableSession = true)]
        public void MailFixedGearStyleX(int customerID, string saveData)
        {
            cmsCustomerDO objCus = new cmsCustomerDO();
            objCus.Id = customerID;
            objCus = new cmsCustomerBL().Select(objCus);


            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            Bike bike = js.Deserialize<Bike>(saveData);

            string body = "";

            body += "<h2>Fixed Gear Style Custom từ khách hàng: " + objCus.FullName + "</h2><br/>";
            body += "<h4>Thông tin khách hàng</h4><br/>";
            body += "<table>";
            body += "<tr><td style='width:150px;'>Họ tên: </td><td>" + objCus.FullName + "</td><tr>";
            body += "<tr><td>Email: </td><td>" + objCus.Email + "</td><tr>";
            body += "<tr><td>Điện thoại: </td><td>" + objCus.Phone + "</td><tr>";
            body += "<tr><td>Địa chỉ: </td><td>" + objCus.Address + "</td><tr>";
            body += "</table>";
            body += "<h4>Thông tin Fixed Gear Style</h4><br/>";
            body += "<table>";
            body += "<tr style='width:150px;'><td>Vành sau: </td><td>" + bike.rimRColor + "</td><tr>";
            body += "<tr><td>Vành trước: </td><td>" + bike.rimFColor + "</td><tr>";
            body += "<tr><td>Lốp sau: </td><td>" + bike.tyreRColor + "</td><tr>";
            body += "<tr><td>Lốp trước: </td><td>" + bike.tyreFColor + "</td><tr>";
            body += "<tr><td>Yên xe: </td><td>" + bike.saddleColor + "</td><tr>";
            body += "<tr><td>Xích: </td><td>" + bike.chainColor + "</td><tr>";
            body += "<tr><td>Khung: </td><td>" + bike.frameColor + "</td><tr>";
            body += "<tr><td>Đĩa: </td><td>" + bike.crankColor + "</td><tr>";
            body += "<tr><td>Tay lái: </td><td>" + bike.gripColor + "</td><tr>";
            body += "<tr><td>Kích thước: </td><td>" + bike.size + "</td><tr>";
            body += "<tr><td>Ghi đông: </td><td>" + bike.handlebar + "</td><tr>";
            body += "</table>";
            string url = "http://xedapgap.com/custom/index.aspx?rimRColor=" + bike.rimRColor.Replace("#", "") + "&rimFColor=" + bike.rimFColor.Replace("#", "") + "&tyreRColor=" + bike.tyreRColor.Replace("#", "") + "&tyreFColor=" + bike.tyreFColor.Replace("#", "") + "&saddleColor=" + bike.saddleColor.Replace("#", "") + "&chainColor=" + bike.chainColor.Replace("#", "") + "&frameColor=" + bike.frameColor.Replace("#", "") + "&gripColor=" + bike.gripColor.Replace("#", "") + "&crankColor=" + bike.crankColor.Replace("#", "");
            body += "<br /><br /><h2 style='color:#333;font-weight:bold;font-size:16px;'>Xem lại xe tại <br/> <span style='color:RED;text-decoration:underline;font-weight:bold;' title='Xem xe khách hàng đã chọn'>" + url + "</span></h2>";
            body += "<p> Ngày gửi:" + DateTime.Now + "</p><br/>";

            Ultility.SendMailX("Fixed gear style - " + objCus.FullName, "Fixed gear style - " + objCus.FullName, body);
        }
        [WebMethod]
        public string SendWithoutLogin(string name, string email, string phone, string address, string saveData)
        {
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            Bike bike = js.Deserialize<Bike>(saveData);

            string body = "";

            body += "<h2>Fixed Gear Style Custom từ khách hàng: " + name + "</h2><br/>";
            body += "<h4>Thông tin khách hàng</h4><br/>";
            body += "<table>";
            body += "<tr><td style='width:150px;'>Họ tên: </td><td>" + name + "</td><tr>";
            body += "<tr><td>Email: </td><td>" + email + "</td><tr>";
            body += "<tr><td>Điện thoại: </td><td>" + phone + "</td><tr>";
            body += "<tr><td>Địa chỉ: </td><td>" + address + "</td><tr>";
            body += "</table>";
            body += "<h4>Thông tin Fixed Gear Style</h4><br/>";
            body += "<table>";
            body += "<tr style='width:150px;'><td>Vành sau: </td><td>" + bike.rimRColor + "</td><tr>";
            body += "<tr><td>Vành trước: </td><td>" + bike.rimFColor + "</td><tr>";
            body += "<tr><td>Lốp sau: </td><td>" + bike.tyreRColor + "</td><tr>";
            body += "<tr><td>Lốp trước: </td><td>" + bike.tyreFColor + "</td><tr>";
            body += "<tr><td>Yên xe: </td><td>" + bike.saddleColor + "</td><tr>";
            body += "<tr><td>Xích: </td><td>" + bike.chainColor + "</td><tr>";
            body += "<tr><td>Khung: </td><td>" + bike.frameColor + "</td><tr>";
            body += "<tr><td>Đĩa: </td><td>" + bike.crankColor + "</td><tr>";
            body += "<tr><td>Tay lái: </td><td>" + bike.gripColor + "</td><tr>";
            body += "<tr><td>Kích thước: </td><td>" + bike.size + "</td><tr>";
            body += "<tr><td>Ghi đông: </td><td>" + bike.handlebar + "</td><tr>";
            body += "</table>";
            string url = "http://xedapgap.com/custom/index.aspx?rimRColor=" + bike.rimRColor.Replace("#", "") + "&rimFColor=" + bike.rimFColor.Replace("#", "") + "&tyreRColor=" + bike.tyreRColor.Replace("#", "") + "&tyreFColor=" + bike.tyreFColor.Replace("#", "") + "&saddleColor=" + bike.saddleColor.Replace("#", "") + "&chainColor=" + bike.chainColor.Replace("#", "") + "&frameColor=" + bike.frameColor.Replace("#", "") + "&gripColor=" + bike.gripColor.Replace("#", "") + "&crankColor=" + bike.crankColor.Replace("#", "");
            body += "<br /><br /><h2 style='color:#333;font-weight:bold;font-size:16px;'>Xem lại xe tại <br/> <span style='color:RED;text-decoration:underline;font-weight:bold;' title='Xem xe khách hàng đã chọn'>" + url + "</span></h2>";
            body += "<p> Ngày gửi:" + DateTime.Now + "</p><br/>";
            if (Ultility.SendMailX("Fixed gear style - " + name, "Fixed gear style - " + name, body))
            {
                return "1";
            }
            else
            {
                return "0";
            }

        }
        [WebMethod(EnableSession = true)]
        public bool MailFixedGearStyle(int customerID, string saveData)
        {
            if (customerID <= 0)
                return false;
            else
            {
                cmsCustomerDO objCus = new cmsCustomerDO();
                objCus.Id = customerID;
                objCus = new cmsCustomerBL().Select(objCus);


                System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
                Bike bike = js.Deserialize<Bike>(saveData);

                string body = "";

                body += "<h2>Fixed Gear Style Custom từ khách hàng: " + objCus.FullName + "</h2><br/>";
                body += "<h4>Thông tin khách hàng</h4><br/>";
                body += "<table>";
                body += "<tr><td>Họ tên: </td><td>" + objCus.FullName + "</td><tr>";
                body += "</table>";
                body += "<h4>Thông tin Fixed Gear Style</h4><br/>";
                body += "<table>";
                body += "<tr><td>Vành sau: </td><td>" + bike.rimRColor + "</td><tr>";
                body += "<tr><td>Vành trước: </td><td>" + bike.rimFColor + "</td><tr>";
                body += "<tr><td>Lốp sau: </td><td>" + bike.tyreRColor + "</td><tr>";
                body += "<tr><td>Lốp trước: </td><td>" + bike.tyreFColor + "</td><tr>";
                body += "<tr><td>Yên xe: </td><td>" + bike.saddleColor + "</td><tr>";
                body += "<tr><td>Xích: </td><td>" + bike.chainColor + "</td><tr>";
                body += "<tr><td>Khung: </td><td>" + bike.frameColor + "</td><tr>";
                body += "<tr><td>Đĩa: </td><td>" + bike.crankColor + "</td><tr>";
                body += "<tr><td>Tay lái: </td><td>" + bike.gripColor + "</td><tr>";
                body += "<tr><td>Kích thước: </td><td>" + bike.size + "</td><tr>";
                body += "<tr><td>Ghi đông: </td><td>" + bike.handlebar + "</td><tr>";
                body += "</table>";
                string url = "http://xedapgap.com/custom/index.aspx?rimRColor=" + bike.rimRColor.Replace("#", "") + "&rimFColor=" + bike.rimFColor.Replace("#", "") + "&tyreRColor=" + bike.tyreRColor.Replace("#", "") + "&tyreFColor=" + bike.tyreFColor.Replace("#", "") + "&saddleColor=" + bike.saddleColor.Replace("#", "") + "&chainColor=" + bike.chainColor.Replace("#", "") + "&frameColor=" + bike.frameColor.Replace("#", "") + "&gripColor=" + bike.gripColor.Replace("#", "") + "&crankColor=" + bike.crankColor.Replace("#", "");
                body += "<br /><br /><h2 style='color:#333;font-weight:bold;font-size:16px;'>Xem lại xe tại <br/> <span style='color:RED;text-decoration:underline;font-weight:bold;' title='Xem xe khách hàng đã chọn'>" + url + "</span></h2>";
                body += "<p> Ngày gửi:" + DateTime.Now + "</p><br/>";

                Ultility.SendMailX("Fixed gear style - " + objCus.FullName, "Fixed gear style - " + objCus.FullName, body);
                return true;
            }
        }

        class Bike
        {
            public string rimRColor, rimFColor, tyreRColor, tyreFColor, saddleColor, chainColor, frameColor, crankColor, gripColor, handlebar;
            public int size;
        }
    }
}
